package com.fcx.workai.jinchang.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fcx.workai.jinchang.R;
import com.hxh.component.business.common.bean.UserCustomerListBean;

import java.util.List;

import com.hxh.component.business.ui.bean.UserCompanyListBean;

/**
 * Created by mardawang on 2017/11/10.
 */

public class CompanyAdapter extends BaseAdapter {
    List<UserCompanyListBean.ItemsBean> companylist;
    List<UserCustomerListBean.ItemsBean> customerlist;
    int cur_index = 0;
    Context mcontext;
    String cur_name;

    public CompanyAdapter(int index,List list, Context context,String name) {
        this.cur_index = index;
        this.mcontext = context;
        this.cur_name = name;
        if(index==0){
            this.companylist = list;
        }else if(index==1){
            this.customerlist = list;
        }
    }

    @Override
    public int getCount() {
        if(cur_index==0 &&companylist!=null){
            return companylist.size();
        }else if(cur_index==1&&customerlist!=null){
            return customerlist.size();
        }else{
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if(cur_index==0 &&companylist!=null){
            return companylist.get(position);
        }else if(cur_index==1&&customerlist!=null){
            return customerlist.get(position);
        }else{
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        MyViewHolder holder = new MyViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_company_list, null);
            holder.tv_company_name = (TextView) convertView.findViewById(R.id.tv_company_name);
            holder.iv_select = (ImageView) convertView.findViewById(R.id.iv_select);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        if(cur_index==0&&companylist!=null){
            holder.tv_company_name.setText(companylist.get(position).getName());
            if(cur_name!=null && cur_name.equals(companylist.get(position).getName())){
                holder.iv_select.setVisibility(View.VISIBLE);
                holder.iv_select.setImageResource(R.drawable.icon_seleted_duigou);
            }else{
                holder.iv_select.setVisibility(View.GONE);
            }
        }else if(cur_index==1&&customerlist!=null){
            holder.tv_company_name.setText(customerlist.get(position).getName());
            if(cur_name!=null && cur_name.equals(customerlist.get(position).getName())){
                holder.iv_select.setVisibility(View.VISIBLE);
                holder.iv_select.setImageResource(R.drawable.icon_seleted_duigou);
            }else{
                holder.iv_select.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    class MyViewHolder {
        private TextView tv_company_name;
        private ImageView iv_select;
    }
}
